/*     */ package com.zimbra.cs.account.ldap.upgrade;
/*     */ 
/*     */ import com.google.common.collect.Lists;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Entry.EntryType;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.ldap.LdapDIT;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.cs.ldap.IAttributes;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapServerType;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions.SearchLdapVisitor;
/*     */ import com.zimbra.cs.ldap.ZAttributes;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import com.zimbra.cs.ldap.ZLdapFilter;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory.FilterId;
/*     */ import com.zimbra.cs.ldap.ZMutableEntry;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class BUG_68831
/*     */   extends UpgradeOp
/*     */ {
/*     */   private static final String ATTR_NAME = "zimbraMailHost";
/*     */   
/*     */   void doUpgrade()
/*     */     throws ServiceException
/*     */   {
/*  44 */     ZLdapContext zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.UPGRADE);
/*     */     try {
/*  46 */       upgradeDLs(zlc);
/*     */     } finally {
/*  48 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */   Description getDescription()
/*     */   {
/*  54 */     return new Description(this, new String[] { "zimbraMailHost" }, new Entry.EntryType[] { Entry.EntryType.DISTRIBUTIONLIST }, null, "randomly picked hostname", String.format("Add home server(%s) on distribution lists", new Object[] { "zimbraMailHost" }));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void upgradeDLs(ZLdapContext zlc)
/*     */     throws ServiceException
/*     */   {
/*  64 */     String[] bases = this.prov.getDIT().getSearchBases(4);
/*     */     
/*  66 */     ZLdapFilterFactory filterFactory = ZLdapFilterFactory.getInstance();
/*     */     
/*  68 */     ZLdapFilter homeServerPresent = filterFactory.fromFilterString(ZLdapFilterFactory.FilterId.LDAP_UPGRADE, filterFactory.presenceFilter("zimbraMailHost"));
/*     */     
/*  70 */     ZLdapFilter homeServerNotPresent = filterFactory.negate(homeServerPresent);
/*     */     
/*     */ 
/*  73 */     String query = filterFactory.andWith(filterFactory.allDistributionLists(), homeServerNotPresent).toFilterString();
/*     */     
/*     */ 
/*  76 */     upgrade(zlc, bases, query);
/*     */   }
/*     */   
/*     */   private void upgrade(ZLdapContext modZlc, String[] bases, String filter)
/*     */     throws ServiceException
/*     */   {
/*  82 */     SearchLdapOptions.SearchLdapVisitor visitor = new Bug68831Visitor(this, modZlc);
/*     */     
/*  84 */     String[] attrs = { "zimbraMailHost" };
/*     */     
/*  86 */     for (String base : bases) {
/*     */       try {
/*  88 */         this.printer.format("\n=== Searching LDAP: base = %s, filter = %s\n", new Object[] { base, filter });
/*  89 */         this.prov.searchLdapOnMaster(base, filter, attrs, visitor);
/*     */       }
/*     */       catch (ServiceException e) {
/*  92 */         this.printer.println("Caught ServiceException while searching " + filter + " under base " + base);
/*  93 */         this.printer.printStackTrace(e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static class Bug68831Visitor extends SearchLdapOptions.SearchLdapVisitor {
/*     */     private final UpgradeOp upgradeOp;
/*     */     private final ZLdapContext modZlc;
/* 101 */     private final List<String> servers = Lists.newArrayList();
/* 102 */     private int index = 0;
/*     */     
/*     */     Bug68831Visitor(UpgradeOp upgradeOp, ZLdapContext modZlc) throws ServiceException {
/* 105 */       super();
/* 106 */       this.upgradeOp = upgradeOp;
/* 107 */       this.modZlc = modZlc;
/*     */       
/* 109 */       for (Server server : upgradeOp.prov.getAllServers()) {
/* 110 */         String serviceHostname = server.getServiceHostname();
/* 111 */         if ((server.hasMailClientService()) && (serviceHostname != null)) {
/* 112 */           this.servers.add(serviceHostname);
/*     */         }
/*     */       }
/*     */       
/* 116 */       if (this.servers.size() == 0) {
/* 117 */         throw ServiceException.FAILURE("no mailclient server running service for assigning home server for distribution lists", null);
/*     */       }
/*     */     }
/*     */     
/*     */     public void visit(String dn, IAttributes ldapAttrs)
/*     */     {
/*     */       try {
/* 124 */         doVisit(dn, (ZAttributes)ldapAttrs);
/*     */       } catch (ServiceException e) {
/* 126 */         this.upgradeOp.printer.println("entry skipped, encountered error while processing entry at:" + dn);
/* 127 */         this.upgradeOp.printer.printStackTrace(e);
/*     */       }
/*     */     }
/*     */     
/*     */     public void doVisit(String dn, ZAttributes ldapAttrs) throws ServiceException {
/* 132 */       this.upgradeOp.printer.println();
/* 133 */       this.upgradeOp.printer.println("Found entry " + dn);
/*     */       
/* 135 */       String homeServer = pickServer();
/* 136 */       ZMutableEntry entry = LdapClient.createMutableEntry();
/* 137 */       entry.setAttr("zimbraMailHost", homeServer);
/* 138 */       this.upgradeOp.replaceAttrs(this.modZlc, dn, entry);
/*     */     }
/*     */     
/*     */     private String pickServer() {
/* 142 */       String serviceHostname = (String)this.servers.get(this.index);
/* 143 */       this.index = ((this.index + 1) % this.servers.size());
/* 144 */       return serviceHostname;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/upgrade/BUG_68831.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */